<?php

/**
 * Base class actions 
 * @property sfI18N $i18n
 * @author mina
 */
class SXActions extends sfActions
{


    /**
     * Create pager for any action
     * @param String $ModelName
     * @param Doctrine_Query $query
     * @param integer $numberOfRecordPerPage
     * @param integer $page
     * @return sfDoctrinePager 
     * @author Mina Gerges
     */
    protected function createPager($ModelName, Doctrine_Query $query, $page = 1, $numberOfRecordPerPage = null)
    {
        /*
         * Issue #:7929
         * Pager refactoring
         */
        if (!$numberOfRecordPerPage)
        {
            $numberOfRecordPerPage = sfConfig ::get('app_default_record_per_page');
        }
        $pager                 = new sfDoctrinePager($ModelName, $numberOfRecordPerPage);
        $pager->setQuery($query);
        $pager->setPage($page);
        $pager->init();
        // that is mean the user try to get page number greater than the last page
        if ($page > $pager->getLastPage())
        {
            // set the rquested page is last page not the requested page 
            $pager->setPage($pager->getLastPage());
            // re set the pager again to get last page
            $pager->init();
        }
        return $pager;
    }

    /**
     * @param type $output
     * @return  sfView::NONE 
     * render json output
     * @author Mina Gerges
     */
    public function renderJson($output, $isEnchoded = false)
    {
        //send the response
        $this->getResponse()->setHttpHeader('Content-Type', 'application/json; charset=utf-8');
        if (!$isEnchoded)
        {
            $output = json_encode($output);
        }
        return $this->renderText($output);
    }


}
